import { Component, OnInit,NgModule,Inject } from '@angular/core';
import{ FormsModule,NgForm }from'@angular/forms';
import {MdDialog,MdDialogRef,MdDialogModule,MD_DIALOG_DATA} from '@angular/material';
import {trigger,state,style,animate,transition} from '@angular/animations';
import {Http, Response,Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Rx';
import {Location} from '@angular/common';
import {ActivatedRoute, Params,Router} from '@angular/router';

import {UserService}from './user.service';
import {User,Message}from './user';
import { PictrueComponent } from '../pictrue/pictrue.component';
import {DataService}from '../data.service';
import {Picture}from '../pictrue/picture';
import {AppRoutingModule}from'../app-routing.module';


@Component({
  selector: 'app-sign',
  templateUrl: './sign.component.html',
  styleUrls: ['./sign.component.css']
})
export class SignComponent implements OnInit{

  isMobileValidated=0;

  user:any;
  inputLength:number;
  code:Message;
  private timer:number;
  private now:number = 10;
  timeText:string = '获取验证码';
  blTime:boolean = true;
  blUser:boolean;
  tip:any;
  //没有验证过
  isMessageValidated=0;
  absurl:any;
  openId:string;
  blmessage:boolean;
  tipMessage:any;
  loading:boolean;
  constructor(
     //声明变量为UserSerice类型
    private userService: UserService,
    private dialog:MdDialog,
    private dataService:DataService,
    private _existUserService:UserService,
    @Inject(ActivatedRoute) private router: ActivatedRoute,
    private _router:Router,
  ){
    this.user = {
      mobile:'',
      code:'',
      flag:''
    }
    console.log('1111');
    // (()=>{
    //   //_router.navigateByUrl('/suc');
    //   console.log('路由跳转前');
    //   _router.navigate(['/suc']);
    //   console.log('路由跳转后');
    // })();
    // ()=>
    //   //_router.navigateByUrl('/suc');
    //   console.log('路由跳转前');
    //   _router.navigate(['/suc']);
    //   console.log('路由跳转后');


  }
  ngOnInit() {
         this.router.params.subscribe((params: Params) => {
           console.log(params.openId);
           this.openId = params.openId;
      });
  }
  getCaptcha(){
      //console.log(this.dataService.text);
        // 问题：应该给变量赋值，目前未赋值；接口变量需要实现接口
        //console.log("点击前："+this.user.mobile);
        //data 是传进来的json
      this.userService.verifyUser(this.user).subscribe(data => {

           console.log("data是什么:"+data);
           console.log(data.flag);
           //手机提示消息
           //console.log(data.message);
          // 获取到返回值为true还是false 验证手机是否存在
           this.blUser = data.flag;
           //console.log(this.blUser);
           // 按钮可不可用 手机号不能为空
           if(!this.blUser){
             if(this.blTime){
               //点击后立马变成无法点击，防止十秒钟内点击
                this.blTime = false;
                //弹出验证码输入框
                 this.dataService.dialogRef = this.dialog.open(PictrueComponent,{data:this.user.mobile,});
                 console.log("重新获取验证码"+this.user.mobile);
                 //弹出框关闭在 在dialog 返回 result事件的
                  this.dataService.dialogRef.afterClosed().subscribe(result => {
                       //倒计时
                       this.timer = setInterval(() => {
                           this.now = this.now-1;
                           if(this.now<=0){
                              clearInterval(this.timer);
                              //按钮上的文字
                              this.timeText="重新获取验证码";
                              this.now=10;
                              //倒计时技术后可以点击
                              this.blTime = true;
                           }
                         }, 1000);
                       }
                     );
                   }
                 }
                 else{
                     this.tip="用户已存在或格式错误，请重新输入";
                 }
                 this.isMobileValidated=1;
                //验证是否通过
                 if(data.falg == false){
                   this.isMobileValidated=2;
                 }
       });
      //  console.log(this.user.flag);
      //验证过

    }

keyupHandler1(event:any) {
      //this.loading = true
      let code = this.user.code;

      if(code.length == 6){
        //console.log("获取前："+code);
        this.userService.checkMessage(this.user).subscribe(data => {
         this.blmessage = data.flag;
         //不管这里flag我赋值是什么，都返回true；这是什么问题！！！！
         console.log("验证手机验证码对错："+data.flag);
         console.log("验证手机号验证码返回信息："+data.message);
         console.log("this.blmessage的值"+this.blmessage);
        this. blmessage = !this.blmessage;
         if(this.blmessage){
             this.tipMessage = "验证码输入错误，请重新输入";
         }
         else {
         }
         this.isMessageValidated=1;
         if(data.flag=true){
           this.isMessageValidated=2;
         }

      });
  }
}
submitCode(){
   //this.loading = true
   if(this.isMobileValidated){
     let tipLoadingMobile0 = "请输入手机号/您输入手机号已经注册";
      console.log("请输入手机号");
   }
   else if(this.isMobileValidated=1){
      let tipLoadingMobile1 = "请输入手机号/您输入手机号已经注册";
     console.log("您输入手机号已经注册");
   }
   else{
     let tipLoadingMobile2 = "验证通过";
     console.log("验证通过");
     ()=>this._router.navigate(['/suc']);
   }

   if(this.dataService.isPictureValidated){
     let tipLoadingPicture0 = "图片验证码未填写或输入错误，请输入";
      console.log("图片验证码未填写，请输入图片验证码");
   }
   else if(this.dataService.isPictureValidated=1){
     let tipLoadingPicture1 = "图片验证码未填写或输入错误，请输入";
     console.log("您输入的图片验证码有误，请输入正确的验证码");
   }
   else{
     let tipLoadingPicture2 = "验证通过";
     console.log("验证通过");
     ()=>this._router.navigate(['/suc']);
   }

   if(this.isMessageValidated){
     let tipLoadingMessage0 = "您未填写手机短信验证码或填写错误，请输入";
      console.log("您未填写验证码，请输入手机短信验证码");
   }
   else if(this.isMessageValidated=1){
     let tipLoadingMessage1 = "您未填写手机短信验证码或填写错误，请输入";
     console.log("您输入手机验证码有误，请输入正确验证码");
   }
   else{
     let tipLoadingMessage2 = "验证通过";
     console.log("验证通过");
     ()=>this._router.navigate(['/suc']);
   }
    this.loading = true;
    this.userService.weixinBind(this.user,this.openId).subscribe(data => {
      console.log("绑定数据测试：");
      console.log(data);
      this.loading = false;
    });
}


//验证表单
//验证表单
nameErrorMsg:any;
  logForm(theForm: NgForm) {
    if (theForm.invalid) {
        if (theForm.controls['name'].errors) {
            this.nameErrorMsg = 'name error:' + JSON.stringify(theForm.controls['name'].errors);
        } else {
            this.nameErrorMsg = null;
        }
    }
    //console.log(theForm.value);
}

}
